﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

public partial class admin_Partners_List : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      ShowData();
    }
  }
  PartnerBLL partnerData = new PartnerBLL();
  
  private void ShowData()
  {
    if (dropIsEnable.SelectedIndex == 0)
    {
        gridView.DataSource = partnerData.GetAll();
    }
    else
    {
      bool isEnable = true;
      if (dropIsEnable.SelectedValue == "2") isEnable = false;
     
      gridView.DataSource = partnerData.GetAll(isEnable);
    }
    gridView.DataBind();

    //Bo mui ten len xuong dau va cuoi
    if (gridView.Rows.Count > 0)
    {
      gridView.Rows[0].Cells[5].Enabled = false;
      gridView.Rows[gridView.Rows.Count - 1].Cells[4].Enabled = false;
    }
  }

  protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    try
    {
      if (e.Row.RowIndex >= 0) //Bo qua dong dau
      {
        int id = Convert.ToInt32(e.Row.Cells[1].Text);

        bool isEnable = partnerData.GetById(id).isEnable;

        //Icon check
        Image img = new Image();
        foreach (Control item in e.Row.Cells[7].Controls)
        {
          if (item.GetType() == typeof(Image))
          {
            img = (Image)item;
            break;
          }
        }
        if (isEnable)
          img.ImageUrl = "~/Images/check.png";
        else
        {
          img.ImageUrl = "~/Images/an.png";

        }
      }
    }
    catch { }
  }

  protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    try
    {
      lbThongBao.Text = "";
      gridView.PageIndex = e.NewPageIndex;
      ShowData();
    }
    catch { }
  }
  protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    try
    {
      lbThongBao.Text = "";
      int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[1].Text);
      string strImage = partnerData.GetById(id).Image;
      //delete image
      Myfunction.DeleteFile("Images/Partner", strImage);
      partnerData.Delete(id);
      Myfunction.ThongBao(lbThongBao,"Thông tin đối tác đã được xóa");

      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao,ex.Message);
    }
  }

  private void Enable(bool isEnable)
  {
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[0].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          if (check.Checked)
          {
            int id = Convert.ToInt32(item.Cells[1].Text);
            partnerData.Enable(id, isEnable);
          }
          break;
        }
      }
    }
    ShowData();
  }
  protected void btnVisible_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    Enable(false);
  }
  protected void btnEnable_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    Enable(true);
  }
  protected void btnXoaChon_Click(object sender, EventArgs e)
  {
    try
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[0].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            if (check.Checked)
            {
              int id = Convert.ToInt32(item.Cells[1].Text);
              //Delete image
              string strImage = partnerData.GetById(id).Image;
              Myfunction.DeleteFile("Images/Partner", strImage);
              partnerData.Delete(id);
            }
            break;
          }
        }
      }
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void linkCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[0].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = true;
          break;
        }
      }
    }
  }
  protected void linkUnCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[0].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = false;
          break;
        }
      }
    }
  }
  protected void dropPosition_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    try
    {
    lbThongBao.Text = "";
    if (e.CommandName.Equals("Up"))
    {
      int index = Convert.ToInt32(e.CommandArgument);
      int idFirst = Convert.ToInt32(gridView.Rows[index].Cells[1].Text);
      int idSecond = Convert.ToInt32(gridView.Rows[index - 1].Cells[1].Text);

      partnerData.SwapSort(idFirst, idSecond);
    }
    else if (e.CommandName.Equals("Down"))
    {
      int index = Convert.ToInt32(e.CommandArgument);
      int idFirst = Convert.ToInt32(gridView.Rows[index].Cells[1].Text);
      int idSecond = Convert.ToInt32(gridView.Rows[index + 1].Cells[1].Text);

      partnerData.SwapSort(idFirst, idSecond);
    }
    ShowData();

    }
    catch (Exception ex)
    {
     Myfunction.ThongBao(lbThongBao,ex.Message);
    }
  }
}